//
//  MainViewController.swift
//  weibo_swift
//
//  Created by dulinlong on 15/6/23.
//  Copyright © 2015年 sheji_56. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabBar
        let mainBar = MainTabBr()
        setValue(mainBar, forKey: "tabBar")
        
        //添加子控制器
        addChildViewControllers()

        //给tabbar的添加按钮添加添加点击事件
        mainBar.composedButton.addTarget(self, action: "composedButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
    }
    //撰写微博的点击
    func composedButtonClicked(sender: UIButton){
        let nav = UINavigationController(rootViewController: ComposeViewController())
        presentViewController(nav, animated: true, completion: nil)
    }

    //将array转为json写到文件下
    private func changeToJson(){
        let dic1 = ["vcName":"HomeTableViewController","title":"首页","imageName":"tabbar_home"]
        let dic2 = ["vcName":"MessageTableViewController","title":"首页","imageName":"tabbar_home"]
        let dic3 = ["vcName":"DiscoverTableViewController","title":"首页","imageName":"tabbar_home"]
        let dic4 = ["vcName":"ProfileTableViewController","title":"首页","imageName":"tabbar_home"]
        
        let array = [dic1,dic2,dic3,dic4] as NSArray

        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last
        
        let fileName = path!.stringByAppendingPathComponent("mainVc.json")
        
        let data = try! NSJSONSerialization.dataWithJSONObject(array, options: NSJSONWritingOptions.PrettyPrinted)
        data.writeToFile(fileName, atomically: true)
    }
   

    ///  添加多个控制器
    private func addChildViewControllers() {
        //从json读取控制器名
        let path = NSBundle.mainBundle().pathForResource("MainVc" , ofType: "json")
        let data = NSData(contentsOfFile: path!)
        do{
        let array = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
            
            //遍历数组的时,必须指明数组中包含对象的类型，包括字典中的格式
            for dict in array as! [[String:String]]{
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        }catch{
            print(error)
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
        
        
        
    }
   
    ///  根据类名字符串创建controller
    ///
    ///  :param: vcName    控制器类名字符串
    ///  :param: title     标题名
    ///  :param: imageName 图片名字
    private  func addChildViewController(vcName:String,title:String,imageName:String){
        
        //根据类名创建控制器
        let fileName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        let controllerClass: AnyClass = NSClassFromString(fileName + "." + vcName)!
        
        let vc = controllerClass.new() as! UIViewController
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        
        let nv = UINavigationController(rootViewController: vc)
        addChildViewController(nv)
        
    }
   

}
